<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>问题列表</title>
    <link rel="stylesheet" type="text/css" href="../css/jquery/jquery-ui-1.8.23.custom.css" media="screen"/>
    <link rel="stylesheet" type="text/css" href="../css/jqgrid/ui.jqgrid.css" media="screen"/>
    <s:head/>
</head>
<script type="text/javascript">
    $(function () {
        var deptId = $("#deptId").val();
        if (deptId == "") {
            deptId = "${deptId}";
        }
        var read_option = [["待查看", "0"], ["已查看", "1"]];
        var ans_option = [["待回复", "0"], ["已回复", "1"]];
        $("#mrdForm").mrdForm({
            title: "查询列表",// 表单标题
            columns: [{
                name: "ischeck",
                label: "管理员查看状态",
                type: "dropdown",
                data: read_option
            }, {
                name: "isanswer",
                label: "管理员回复状态",
                type: "dropdown",
                data: ans_option
            }

            ],
            buttons: {
                'custom': [{
                    id: "que", value: "提问", click: function () {
                        initForm();
                        $("#dialog").dialog({title: "问题"}, {width: 700}, {modal: true});
                    }
                },
                    {
                        id: "ans", value: "回复", click: function () {
                        var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
                        if (!selected_rowid || "" == selected_rowid || null == selected_rowid) {
                            alert("请选择需要回复的问题");
                            return;
                        }
                        var rowData = $("#columnGrid").getRowData(selected_rowid);
                        var id = rowData.id;
                        location.href = "ans-list?ques.id=" + id;
                        ;
                    }
                    }
                ]
            },
            grid: {
                id: "columnGrid",
                url: "ques-quesList",
                colNames: ["id", "问题", "查看", "回复", "回复记录数", "提问时间"],
                colModel: [{
                    name: 'id',
                    index: 'id',
                    hidden: true,
                    align: "center"
                }, {
                    name: 'contents',
                    index: 'contents',
                    label: "问题",
                    width: 100,
                    align: "center"
                }, {
                    name: 'ischeck',
                    index: 'ischeck',
                    label: "查看",
                    width: 50,
                    align: "center",
                    formatter: function (cellvalue, options, row) {
                        if (cellvalue == "0") {
                            return "待管理员查看";
                        } else if (cellvalue == "1") {
                            return "管理员已查看";
                        }
                    }
                }, {
                    name: 'isanswer',
                    index: 'isanswer',
                    label: "回复",
                    width: 20,
                    align: "center",
                    //字段值的显示格式转换
                    formatter: function (cellvalue, options, row) {
                        if (cellvalue == "0") {
                            return "待管理员回复";
                        } else if (cellvalue == "1") {
                            return "管理员已回复";
                        }
                    }
                }, {
                    name: 'anstotal',
                    index: 'anstotal',
                    label: "回复记录数",
                    width: 50,
                    align: "center"
                }, {
                    name: 'sendtime',
                    index: 'sendtime',
                    label: "提问时间",
                    width: 80,
                    align: "center",
                    formatter: 'date',
                    formatoptions: {srcformat: 'Y-m-d H:i', newformat: 'Y-m-d'}
                }],
                gridComplete: function () {
                    var ids = jQuery("#columnGrid").jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var id = ids[i];
                        var value = jQuery("#columnGrid").jqGrid("getRowData", id);
                        focusNum = "";
                        focusNum = "<a href='#' style='color:#f60' alt='点击查看回复记录'>查看" + ((value.anstotal == null || value.anstotal == "" || value.anstotal == 0) ? "(0)" : "(" + value.anstotal + ")") + "</a>";
                        if (value.anstotal == null || value.anstotal == "" || value.anstotal == 0) {
                            focusNum = "<a href='#' style='color:#f60' alt='点击查看回复记录'>查看(0)</a>";
                        } else {
                            focusNum = "<a href='#' style='color:#f60' alt='点击查看回复记录' onclick=\"details(\'" + id + "\')\">查看(" + value.anstotal + ")</a>";
                        }
                        jQuery("#columnGrid").jqGrid('setRowData', id, {anstotal: focusNum});
                    }
                },
                title: "问题列表",
                //sortname:"ctime",
                //rowNum:5,
                jsonReader: {
                    //从哪个json对象中取值
                    root: "queses"
                },
                onSelectRow: function (id) {
                    //parent.document.getElementById("rightFrame").src="../base/company-mainUser?dept.id="+id;
                },
                ondblClickRow: function (id) {
                    //location.href = "company-manageUser?user.id="+id+"&dept.id="+$("#deptId").val();
                }
            }
        });
        $("#mrdForm").mrdForm("resize");
    });

    function details(id) {
        location.href = "ans-list?ques.id=" + id;
    }

    function initForm() {
        //初始化表单验证引擎
        $("#quesForm").validationEngine("attach", {
            //消息位置
            promptPosition: "bottomRight",
            scroll: false
        });
        //初始化ajaxForm参数
        var options = {
            //提交方式
            type: "POST",
            //提交数据格式
            dataType: "json",
            //在提交之前做的操作
            beforeSubmit: function () {
                return $("#quesForm").validationEngine("validate");
            },
            //成功后做的操作
            success: function (responseText, statusText, xhr, $form) {
                $("#dialog").dialog("close");
                alert(responseText.msg);
                location.reload(true);
            }
        };
        $('#quesForm').ajaxForm(options);
    }

    function submitTheForm() {
        $("#quesForm").submit();
    }

    function close() {
        $('#dialog').dialog("close");
    }
</script>
<body>
<div id="mrdForm"></div>
<div id="dialog" title="Basic dialog" style="display: none;">
    <form action="ques-save" id="quesForm">
        <div class="ui-widget">
            <table class="ui-widget-content">
                <tr>
                    <td class="td_right">问题描述：</td>
                    <td colspan="3">
                        <textarea class="validate[required]" rows="3" cols="40" name="ques.contents"></textarea>*
                </tr>
                <tr>
                    <td colspan="4" align="center" class="bts">
                        <input type="button" onclick="submitTheForm()" name="btn" value="提问"/>
                        <input type="button" onclick="close();" name="btn" value="取消"/>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</div>
</body>
</html>
